package dao.hibernate;

import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.io.Serializable;

import dao.IGenericDAO;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * Implements the generic CRUD data access operations using Hibernate.
 * <p>
 * To write a DAO, subclass and parameterize this class with your entity.
 * Of course, assuming that you have a traditional 1:1 appraoch for
 * Entity:DAO design. 
 *
 */
public abstract class GenericDAOImpl<T,ID extends Serializable> 
        extends HibernateDaoSupport implements IGenericDAO<T, ID> {
    private Class<T> persistentClass;

    @SuppressWarnings("unchecked")
    public GenericDAOImpl() {
        this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                .getGenericSuperclass()).getActualTypeArguments()[0];
        
    }

    public Class<T> getPersistentClass() {
        return persistentClass;
    }
    
    public void save(T entity) {
        getHibernateTemplate().save(entity);
    }

    public void update(T entity) {
        getHibernateTemplate().update(entity);

    }

    public void delete(T entity) {
        getHibernateTemplate().delete(entity);
    }

    @SuppressWarnings("unchecked")    
    public T get(ID id) {
        return (T) getHibernateTemplate().get(getPersistentClass(), id);
    }
}
